package other.edan.erp.product;

import com.rkhd.platform.sdk.data.model.Product;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;
import other.edan.erp.k3.SyncProduct;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 新增产品后: 根据编号重置产品数据
 * @Author 黎炎松
 * 2024-06-26 11:35
 */
public class ProductAddAfterTrigger implements Trigger {

    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "新增产品后: 根据编号重置产品数据 ==> ";

    @Override
    public TriggerResponse execute(TriggerRequest request) {
        logger.debug(LOGGER_MSG + " 触发开始 ");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<Product> productList = new ArrayList<>();
        for (XObject xObject : dataList) {
            Product product = (Product) xObject;
            productList.add(product);
            dataResultList.add(new DataResult(true, "", xObject));
        }
        resetProductInfoByNumber(productList.get(0));
        return new TriggerResponse(true, "处理完成", dataResultList);
    }

    /**
     * 新增产品后: 根据编号重置产品数据
     * @param product
     */
    public void resetProductInfoByNumber(Product product){
        // 调用接口进行更新
        logger.debug(LOGGER_MSG + "处理开始: " + product);
        Long entityType = product.getEntityType();
        if (entityType != 3351183423806053L) {
            logger.debug(LOGGER_MSG + "当前类型无需处理");
            return;
        }

        String fNumber__c = product.getFNumber__c();
        SyncProduct syncProduct = new SyncProduct();
        syncProduct.startSyncProductData(fNumber__c);
        logger.debug(LOGGER_MSG + "处理结束");
    }


}
